from celery import Celery

# 创建Celery实例

import os
from dotenv import load_dotenv

load_dotenv()

class CeleryConfig:
    broker_url = os.getenv("CELERY_BROKER_URL", "redis://:redhat@172.16.18.131:6379/0")
    result_backend = os.getenv("CELERY_BACKEND_URL", "redis://:redhat@172.16.18.131:6379/1")
    task_serializer = "json"
    result_serializer = "json"
    accept_content = ["json"]
    timezone = "UTC"
    enable_utc = True
    # 任务超时设置（秒）
    task_time_limit = 30 * 60  # 30分钟
    # 任务软超时（发送SIGTERM）
    task_soft_time_limit = 25 * 60  # 25分钟
    # 任务结果过期时间（秒）
    result_expires = 24 * 60 * 60  # 24小时
    # Worker并发设置
    worker_concurrency = 4
    # 任务确认设置
    task_acks_late = True  # 任务完成后才确认
    task_reject_on_worker_lost = True  # Worker丢失时重新排队




Celery_app = Celery('tasks',
                    include=['app.tasks.test_model.tasks']
                    )


# 应用配置
Celery_app.config_from_object(CeleryConfig)

# 自动发现任务
Celery_app.autodiscover_tasks()

